<!-- 新增和编辑弹窗 -->
<template>
  <a-modal
    :width='800'
    :visible='visible'
    :confirm-loading='loading'
    :title="isUpdate ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible='updateVisible'
    @ok='save'
  >
    <a-alert message="牛群标识需要选择，找不到牛群标识请联系客服进行添加。" type="warning" />
    <a-form
      layout='horizontal'
      ref='form'
      :model='form'
      :rules='rules'
      :label-col='{ md: { span: 4 }, sm: { span: 24 } }'
      :wrapper-col='{ md: { span: 20 }, sm: { span: 24 } }'
    >

      <a-form-item label='牛群名称:' name='herdName'>
        <a-input v-model:value='form.herdName' placeholder='请输入牛群名称' allow-clear autocomplete='off'/>
      </a-form-item>

      <a-form-item label="请选择统一牛群标识:" name="herdCode">
        <a-select
          show-search
          v-model:value="form.herdCode"
          placeholder="请选择统一牛群标识"
          style="width: 100%"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
        >
          <a-select-option v-for="item in herdCodeList" :key="item.herdCode" >
            {{ item.herdCode }}
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import {CattleHerdApi} from '@/api/property/Cattlemanager/CattleHerdApi';
import { message } from 'ant-design-vue';
import {SysCattleHerdCodeApi} from "@/api/property/cattlemanager/SysCattleHerdCodeApi";
export default {
  name: 'CattleManagerCattleHerdEdit',
  components: {},
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  created() {
    if (this.data) {
      this.form = Object.assign({}, this.data);
      this.isUpdate = true;
    } else {
      this.isUpdate = false;
    }
    SysCattleHerdCodeApi.list({}).then(result => {
      this.herdCodeList = result;
      console.log(this.herdCodeList)
    }).catch((err) => {
      console.log(err)
    });
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        herdName: [{required: true, message: '请输入牛群名称', type: 'string', trigger: 'blur'}]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      herdCodeList : []
    };
  },
  methods: {
    /**
     * 保存和编辑
     *
     * @author cancan
     * @date 2023/03/18 20:10
     */
    async save() {
      console.log(this.form);
      // 校验表单
      await this.$refs.form.validate();

      // 修改加载框为正在加载
      this.loading = true;

      let result = null;

      // 执行编辑或修改
      if (this.isUpdate) {
        result = CattleHerdApi.edit(this.form);
      } else {
        result = CattleHerdApi.add(this.form);
      }
      result
        .then(result => {
          // 移除加载框
          this.loading = false;

          // 提示添加成功
          message.success(result.message);

          // 如果是新增，则form表单置空
          if (!this.isUpdate) {
            this.form = {};
          }

          // 关闭弹框，通过控制visible的值，传递给父组件
          this.updateVisible(false);

          // 触发父组件done事件
          this.$emit('done');
        })
        .catch(() => {
          this.loading = false;
        });
    },

    /**
     * 更新编辑界面的弹框是否显示
     *
     * @param value true-显示，false-隐藏
     * @author cancan
     * @date 2023/03/18 20:10
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
};
</script>
